package com.kitty.game.rank.service.handler;

import com.kitty.game.pet.model.Pet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.equip.model.EquipField;
import com.kitty.game.rank.model.PersonInfo;
import com.kitty.game.rank.message.RespRankTopUser;
import com.kitty.game.rank.model.RankType;
import com.kitty.game.rank.model.pojo.DataUnit;
import com.kitty.game.rank.model.pojo.RankGroupListData;
import com.kitty.game.rank.model.record.PetAttribRankRecord;
import com.kitty.game.rank.model.record.RankRecord;
import com.kitty.game.utils.Const;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

/**宠物排行榜处理类*/
@Component
@Slf4j
public class PetRankHandler extends RangeRankHandler {
    private List<Short> ranges = new ArrayList<>();

    @PostConstruct
    private void initRange() {
        ranges.add(Const.RANK_PET_MARTIAL);
        ranges.add(Const.RANK_PET_PHY_POWER);
        ranges.add(Const.RANK_PET_MAG_POWER);
        ranges.add(Const.RANK_PET_DEFENSE);
        ranges.add(Const.RANK_PET_SPEED);
        ranges.add(Const.RANK_PET_MONTH_MARTIAL);
        ranges.add(Const.RANK_PET_ZHANLI);
    }

    /**从数据库加载排行榜数据*/
    @Override
    public void init() {
        Dao dao = SpringUtils.getBean(Dao.class);
        RankGroupListData<PetAttribRankRecord> rankGroupListData = dao.fetch(RankGroupListData.class, RankType.PET_ATTRIB.name());
        if (rankGroupListData != null) {
            rankGroupListData.doAfterInit(PetAttribRankRecord.class);

            List<DataUnit> rankData = rankGroupListData.getRankData();
            for (DataUnit dataUnit : rankData) {
                String range = dataUnit.getKey();
                for (RankRecord record : dataUnit.getRecords()) {
                    PetAttribRankRecord petRecord = (PetAttribRankRecord) record;
                    update(range, petRecord.getGid(), record);
                }
            }
        }
    }

    /**保存排行榜数据*/
    public void save() {
        List<DataUnit> rankData = new ArrayList<>();
        for (Short type : ranges) {
            String range = getRange(type);
            List<RankRecord> records = getRecordsFrom(range, 0, getRankSize());
            if (records == null || records.size() == 0) {continue ;}

            DataUnit dataUnit = new DataUnit(range, records);
            rankData.add(dataUnit);
        }

        if (rankData.size() <= 0) {return ;}

        RankGroupListData<PetAttribRankRecord> rankGroupListData = new RankGroupListData<>(RankType.PET_ATTRIB, rankData);
        rankGroupListData.doBeforeSave();
        Dao dao = SpringUtils.getBean(Dao.class);
        dao.insertOrUpdate(rankGroupListData);
    }

    @PreDestroy
    private void shutDown() {
        save();
        log.error("[{}]保存数据后关闭", this.getClass().getSimpleName());
    }

    public void update(short rankType, Pet pet, int value) {
        boolean update = update(getRange(rankType), pet.getGid(), new PetAttribRankRecord(pet.getGid(), pet.getName(), pet.getLevel(), pet.getHostName(), value,pet.getType()));
        /**加入排行榜成功后，加入显示信息*/
        if (update) {
            List<EquipField> list = new ArrayList<>();
            list.addAll(pet.getAllFields());
            SpringUtils.getRankService().addRankView(pet.getGid(), list);
        }
    }

    private String getRange(short rankType) {
        return String.valueOf(rankType);
    }

    public RespRankTopUser getRankResponse(short type) {
        RespRankTopUser respRankTopUser = new RespRankTopUser();
        respRankTopUser.setType(type);
        respRankTopUser.setCookie(new Long(System.currentTimeMillis() / 1000).intValue());
        respRankTopUser.setList(new ArrayList<>());
        short rankType = getRankType(type);
        List<PersonInfo> personInfoList = getPersonInfoList(rankType, getRecordsFrom(type),type);
        respRankTopUser.setList(personInfoList);
        respRankTopUser.setSize((short) respRankTopUser.getList().size());
        return respRankTopUser;
    }

    public List<RankRecord> getRecordsFrom(short type) {
        short rankType = getRankType(type);
        String range = getRange(rankType);
        return getRecordsFrom(range, 0, getRankSize());
    }

    private short getRankType(short type) {
        switch (type) {
            case 301:
                return Const.RANK_PET_MARTIAL;
            case 302:
                return Const.RANK_PET_PHY_POWER;
            case 303:
                return Const.RANK_PET_MAG_POWER;
            case 304:
                return Const.RANK_PET_SPEED;
            case 305:
                return Const.RANK_PET_DEFENSE;
            case 306:
                return Const.RANK_PET_MONTH_MARTIAL;
            case 307:
                return Const.RANK_PET_ZHANLI;
            case 1305:
                return Const.RANK_PET_ZHANLI;
            case 1301:
                return Const.RANK_PET_PHY_POWER;
            case 1302:
                return Const.RANK_PET_MAG_POWER;
            case 1303:
                return Const.RANK_PET_SPEED;
            case 1304:
                return Const.RANK_PET_DEFENSE;

        }
        return 0;
    }

    private List<PersonInfo> getPersonInfoList(short rankType, List<RankRecord> records,short type) {
        List<PersonInfo> list = new ArrayList<>(records.size());
        for (RankRecord rankRecord : records) {
            PetAttribRankRecord petAttribRankRecord = (PetAttribRankRecord)rankRecord;
            if(type>1000){
                //查询鬼宠
                if(petAttribRankRecord.getType() == 6 || petAttribRankRecord.getType() == 7 || petAttribRankRecord.getType() == 8 ){
                    PersonInfo personInfo = new PersonInfo();
                    personInfo.setList(new ArrayList<>());
                    personInfo.getList().add(new FiedValue(306, petAttribRankRecord.getGid()));
                    personInfo.getList().add(new FiedValue(1, petAttribRankRecord.getName()));
                    personInfo.getList().add(new FiedValue(31, petAttribRankRecord.getLevel()));
                    personInfo.getList().add(new FiedValue(176, petAttribRankRecord.getRoleName()));
                    personInfo.getList().add(getAttribFiedValue(rankType, petAttribRankRecord));
                    list.add(personInfo);
                }
            }else{
                PersonInfo personInfo = new PersonInfo();
                personInfo.setList(new ArrayList<>());
                personInfo.getList().add(new FiedValue(306, petAttribRankRecord.getGid()));
                personInfo.getList().add(new FiedValue(1, petAttribRankRecord.getName()));
                personInfo.getList().add(new FiedValue(31, petAttribRankRecord.getLevel()));
                personInfo.getList().add(new FiedValue(176, petAttribRankRecord.getRoleName()));
                personInfo.getList().add(getAttribFiedValue(rankType, petAttribRankRecord));
                list.add(personInfo);
            }
        }
        return list;
    }

    private FiedValue getAttribFiedValue(short rankType, PetAttribRankRecord petAttribRankRecord) {
        FiedValue fiedValue = new FiedValue();
        fiedValue.setVT((byte) 3);
        switch (rankType) {
            case Const.RANK_PET_MARTIAL:
                fiedValue.setType((short) 62);// 武学
                fiedValue.setValue(petAttribRankRecord.getAttrbValue());
                break;
            case Const.RANK_PET_PHY_POWER:
                fiedValue.setType((short) 3);// 物伤
                fiedValue.setValue(petAttribRankRecord.getAttrbValue());
                break;
            case Const.RANK_PET_MAG_POWER:
                fiedValue.setType((short) 10);// 法伤
                fiedValue.setValue(petAttribRankRecord.getAttrbValue());
                break;
            case Const.RANK_PET_SPEED:
                fiedValue.setType((short) 14);// 速度
                fiedValue.setVT((byte) 2);
                fiedValue.setValue(petAttribRankRecord.getAttrbValue());
                break;
            case Const.RANK_PET_DEFENSE:
                fiedValue.setType((short) 8);// 防御
                fiedValue.setValue(petAttribRankRecord.getAttrbValue());
                break;
            case Const.RANK_PET_MONTH_MARTIAL:
                fiedValue.setType((short) 466);// 月武学
                fiedValue.setValue(petAttribRankRecord.getAttrbValue());
                break;
            case Const.RANK_PET_ZHANLI:
                fiedValue.setType((short) 493);// 战力
                fiedValue.setValue(petAttribRankRecord.getAttrbValue());
                break;
        }

        return fiedValue;
    }

    /**重置月排行榜*/
    public void resetMonthRank() {
        String range = getRange(Const.RANK_PET_MONTH_MARTIAL);
        resetRank(range);
    }

    /**
     * 删除宠物后更新排行榜
     */
    public void updateAfterRemove(Pet pet) {
        for (Short type : ranges) {
            remove(getRange(type), pet.getGid());
        }
    }
}
